package com.cskaoyan.controller.admincontroller;

import com.cskaoyan.util.ValidationUtil;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.topic.vo.AdminTopicReadVo;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.adminservice.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 楚夏三分
 * @datetime 2022/12/30 9:08
 * @description: TODO:
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    @RequiresPermissions("admin:topic:list")
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo info, String title, String subtitle) {
        CommonData<MarketTopic> data = topicService.list(info, title, subtitle);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("admin:topic:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic) {
        topicService.delete(marketTopic);
        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:topic:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated MarketTopic marketTopic, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketTopic data = topicService.create(marketTopic);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("admin:topic:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated MarketTopic marketTopic,BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketTopic data = topicService.update(marketTopic);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("admin:topic:read")
    @GetMapping("read")
    public BaseRespVo read(Integer id) {
        AdminTopicReadVo data = topicService.read(id);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions("admin:topic:batch-delete")
    @PostMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody Map map){
        List<Integer> ids = (List<Integer>) map.get("ids");
        topicService.batchDelete(ids);
        return BaseRespVo.ok();
    }

}
